<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript" src="../js/axios.min.js"></script>
    <script type="text/javascript" src="../js/vue-router.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/animate.css">
    <style>
        .v-enter,
        .v-leave-to {
            opacity: 0;
            transform: translateX(200px);
        }
        
        .v-enter-active,
        .v-leave-active {
            transition: all 0.5s ease;
        }
    </style>
</head>

<body>
    <div id="app" class="container">
        <div class="row">
            <button class="btn btn-default col-md-6" @click="login">登录</button>
            <button class="btn btn-default col-md-6" @click="register">注册</button>
        </div>
        <div class="row">
            <router-link to="/login" tag="button" class="btn-danger">login</router-link>
            <router-link to="/register" tag="button" class="btn-warning">register</router-link>
            <transition mode="out-in">
                <router-view></router-view>
            </transition>
        </div>
    </div>

    <template id="loginTemp">
        <form>
            <h3>登录界面</h3>
            <div class="form-group">
                <label for="usernameLogin">username</label>
                <input type="text" id="usernameLogin" class="form-control" placeholder="enter your username">
            </div>
            <div class="form-group">
                <label for="passwordLogin">password</label>
                <input type="password" id="passwordLogin" class="form-control" placeholder="enter your password">
            </div>
            <button class="btn btn-default col-md-12">登录</button>
        </form>
    </template>

    <template id="registerTemp">
        <form>
            <h3>注册界面</h3>
            <div class="form-group">
                <label for="usernameRegister">username</label>
                <input type="text" id="usernameRegister" class="form-control" placeholder="enter your username">
            </div>
            <div class="form-group">
                <label for="passwordRegister">password</label>
                <input type="password" id="passwordRegister" class="form-control" placeholder="enter your password">
            </div>
            <button class="btn btn-primary col-md-12">注册</button>
        </form>
    </template>

    <script>
        var loginTemp = {
            template: '#loginTemp'
        }

        var registTemp = {
            template: '#registerTemp'
        }

        // 当导入vue-router包后，在window全局对象中就有了一个路由的构造函数VueRouter，在new路由对象的时候
        // 可以为构造函数传递一个配置对象
        var routerObj = new VueRouter({
            // 这个配置对象中的routes表示路由匹配规则
            routes: [
                // 每个路由规则都是一个对象，这个对象有两个必须的属性
                // 1、path：表示监听那个路由链接地址
                // 2、component：表示如果路由是前面匹配到的path，则展示component属性对应的组件
                //      【注意：component的属性值必须是组件的模板对象，不能是组建的引用名称】
                {
                    path: '/login',
                    component: loginTemp
                }, {
                    path: '/register',
                    component: registTemp
                }, {
                    path: '/',
                    redirect: '/login'
                }
            ]
        });


        var vm = new Vue({
            el: '#app',
            data: {},
            methods: {
                login() {
                    this.$router.push({
                        path: '/login'
                    })
                },
                register() {
                    this.$router.push({
                        path: '/register'
                    })
                }
            },
            components: {},
            router: routerObj // 将路由规则注册到vm实例对象上，用于监听URL的变换而展示某个组件
        });
    </script>
</body>

</html>